// view/index/mingpian/mingpian.js
let app = getApp()
let yuming = app.globalData.yuming
import QRCode from '../../utils/weapp-qrcode.js';
const QR = require('../../utils/qrcode.js')
import {
  base64src
} from '../../utils/base64.js'
const request = require('../../config/request');
var context = null;
Page({
  /**
   * 页面的初始数据
   */
  ...request,
  data: {
    yuming: yuming,
    heights: "",
    title: '',
    ewmImg: '',
    shopName: '',
    jgs: '',
    qrcodeURL: "",
    codeText: '',
    // shouma: yuming + 'static/applet/images/index/erwei.png'
    posterWidth: 346,
    posterHeight: 528,
    qrSize: 208,
    rpx: 1,
    hangyeID: '',
    purte: ''
  },
  // 商户信息
  shxx() {
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    this.myRequest(yuming + 'Besall/findShopInfos', {
      shopId: wx.getStorageSync('shopId'),
    }, "POST").then(res => {
      wx.hideLoading()
      var data = res.data.data
      this.setData({
        jgs: data.head_title,
      })
      this.setData({
        codeText: 'https://msion.86wky.com/H5alipay/index.html?shopId=' + data.id + '&type=0'
      })
      console.log(data.id)
      this.drawImg();
    })
  },
  pter() {
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    this.myRequest(yuming + 'Besall/fromWxletQrimg', {
      shopId: wx.getStorageSync('shopId'),
    }, "POST").then(res => {
      this.setData({
        purte: res.data.data
      })
    })
  },
  // 绘制二维码
  drawImg: function () {
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    // var imgData = QR.drawImg(this.data.codeText, {
    //   typeNumber: 4,
    //   errorCorrectLevel: 'M',
    //   size: 500
    // })
    // this.setData({
    //   qrcodeURL: imgData
    // })
    wx.hideLoading();
    this.draw();
  },
  // 绘制图片
  // drawCanvasImg() {
  //   var that = this
  //   var myCanvasWidth = 200 //为了让图片满铺页面
  //   var myCanvasHeight = 200
  //   context = wx.createCanvasContext('canvas');
  //   wx.getSystemInfo({
  //     success: function (res) {
  //       base64src(that.data.qrcodeURL, res => {
  //         console.log(res) // 返回图片地址
  //         context.drawImage(res, 0, 0, 200, 200); //画布绘制图片
  //         context.draw();
  //       });
  //     },
  //   })
  // },
  // 保存二维码
  saveImg: function () { //保存图片
    wx.showLoading({
      title: '保存中...',
    });
    wx.canvasToTempFilePath({
      canvasId: 'poster',
      fileType: 'png',
      success: function (res) {
        console.log(res)
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success(res) {
            console.log(res)
            wx.hideLoading();
            wx.showToast({
              title: '保存成功',
            });
            // //存入服务器
            // wx.uploadFile({
            //  url: \'\', //接口地址
            //  filePath: res.tempFilePath,
            //  name: \'file\',
            //  formData: {                 //HTTP 请求中其他额外的 form data 
            //   \'user\': \'test\'
            //  },
            //  success: function (res) {
            //   console.log(res);

            //  },
            //  fail: function (res) {
            //   console.log(res);
            //  },
            //  complete: function (res) {
            //  }
            // });
          },
          fail() {
            wx.hideLoading()
          },
        })
      }
    })
  },
  draw() {
    let that = this;
    // var bejntt = yuming + 'static/applet/images/linshi/mabei.jpg'  //商户
    var bejntt = yuming + 'static/applet/images/linshi/qiyema.png' //企业
    var bejntts = ''
    var qrcodebgs = yuming + 'static/applet/images/linshi/qrcodebg.png'
    var qrcodebg = ''
    wx.downloadFile({
      url: bejntt,
      success: function (res) {
        console.log(res)
        bejntts = res.tempFilePath
        wx.downloadFile({
          url: qrcodebgs,
          success(resa) {
            qrcodebg = resa.tempFilePath
            wx.getSystemInfo({
              success: function (res) {

                wx.downloadFile({
                  url: that.data.purte,
                  success(aa) {
                    console.log(aa)
                    const ctx = wx.createCanvasContext('poster');
                    if (that.data.hangyeID != 6) {
                      ctx.drawImage(qrcodebg, 0, 0, that.data.posterWidth * that.data.rpx, that.data.posterHeight * that.data.rpx);
                    }
                    if (that.data.hangyeID == 6) {
                      console.log(bejntts)
                      ctx.drawImage(bejntts, 0, 0, that.data.posterWidth * that.data.rpx, that.data.posterHeight * that.data.rpx);
                    }
                    ctx.drawImage(aa.tempFilePath, that.data.posterWidth * that.data.rpx - that.data.qrSize * that.data.rpx - 69 * that.data.rpx, that.data.posterHeight * that.data.rpx - 321 * that.data.rpx, that.data.qrSize * that.data.rpx, that.data.qrSize * that.data.rpx);
                    ctx.fillStyle = '#dcdcdc'
                    if (that.data.hangyeID != 6) {
                      ctx.font = `15px 微软雅黑`;
                    }
                    if (that.data.hangyeID == 6) {
                      ctx.font = `16px 微软雅黑`;
                    }
                    const jgsX = 350 / 2 * that.data.rpx
                    const jgsY = 64 * that.data.rpx
                    ctx.setTextAlign('center');
                    ctx.fillText(that.data.jgs, jgsX, jgsY);
                    if (that.data.hangyeID != 6) {
                      ctx.fillStyle = '#999'
                    }
                    if (that.data.hangyeID == 6) {
                      ctx.fillStyle = '#696969'
                    }
                    ctx.font = `15px 微软雅黑`;
                    const shopNameX = 350 / 2 * that.data.rpx
                    var shopNameY = ''
                    if (that.data.hangyeID != 6) {
                      shopNameY = 460 * that.data.rpx
                    }
                    if (that.data.hangyeID == 6) {
                      shopNameY = 455 * that.data.rpx
                    }
                    ctx.setTextAlign('center');
                    ctx.fillText(that.data.shopName, shopNameX, shopNameY);
                    ctx.draw();
                  }
                })

              },
            })
          }
        })
      }
    })

  },
  backs() {
    wx.navigateBack({
      delta: 0
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      title: options.title,
      shopName: wx.getStorageSync('shopname')
    })
    let that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.data.rpx = res.windowWidth / 375;
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      hangyeID: wx.getStorageSync('stryId')
    })
    if (this.data.hangyeID == 6) {
      this.setData({
        title: '企业码'
      })
    }
    if (this.data.hangyeID != 6) {
      this.setData({
        title: '收款码'
      })
    }
    this.shxx();
    this.pter()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})